package com.shsoft.task;

import com.shsoft.MonitorApplication;
import com.shsoft.component.BarComponent;
import com.shsoft.component.BaseComponent;
import com.shsoft.component.DataComponent;
import com.shsoft.config.PCStatusConstant;
import com.shsoft.entity.HardWareInfo;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage;
import org.springframework.util.StringUtils;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Map;

/**
 * 发送日常消息的任务
 */
public class ProcessingComponentsTask implements Runnable{

    @Override
    public void run() {
        while (true){
            try {
                ArrayList<BaseComponent> componentList = MonitorApplication.mainController.communicationComponentList;
                Map<String, WritableImage> componentBaseImageMap = MonitorApplication.mainController.componentBaseImageMap;
                if(componentList.size() > 0 && componentBaseImageMap.size() > 0){
                    boolean flag = true;
                    for (BaseComponent component : componentList) {
                        if(component.getType() == BaseComponent.ComponentType.BAR){
                            BarComponent barComponent = (BarComponent) component;
                            if(PCStatusConstant.weatherList.contains(barComponent.getName())){ // 城市温度、湿度的条形组件
                                if(StringUtils.isEmpty(HardWareInfo.cityWeatherText)){ // 上位机刚启动，还没有查询出天气信息
                                    flag = false;
                                }
                                if(!HardWareInfo.refreshAll){
                                    continue;
                                }
                            }
                            Canvas canvas = new Canvas(barComponent.getTotalWidth(), barComponent.getTotalHeight());
                            GraphicsContext gc = canvas.getGraphicsContext2D();
                            gc.drawImage(componentBaseImageMap.get(PCStatusConstant.PRE_BAR_COMPONENT + barComponent.getName()), 0, 0);
                            barComponent.drawComponentToSmallCanvas(gc);
                            Platform.runLater(() -> {
                                BufferedImage image = SwingFXUtils.fromFXImage(canvas.snapshot(null, null), null);
                                MonitorApplication.taskExecutor.execute(new ProcessingImageDataTask(image, barComponent.getBorderX(), barComponent.getBorderY(), false));
                            });
                        }
                        if(component.getType() == BaseComponent.ComponentType.DATA){
                            DataComponent dataComponent = (DataComponent) component;
                            if(PCStatusConstant.dateList.contains(dataComponent.getName()) || PCStatusConstant.weatherList.contains(dataComponent.getName())){
                                if(StringUtils.isEmpty(HardWareInfo.cityWeatherText)){ // 上位机刚启动，还没有查询出天气信息
                                    flag = false;
                                }
                                if(!HardWareInfo.refreshAll){
                                    continue;
                                }
                            }
                            Canvas canvas = new Canvas(dataComponent.getBorderWidth(), dataComponent.getBorderHeight());
                            GraphicsContext gc = canvas.getGraphicsContext2D();
                            gc.drawImage(componentBaseImageMap.get(PCStatusConstant.PRE_DATA_COMPONENT + dataComponent.getName()), 0, 0);
                            dataComponent.drawComponentToSmallCanvas(gc);
                            Platform.runLater(() -> {
                                BufferedImage image = SwingFXUtils.fromFXImage(canvas.snapshot(null, null), null);
                                MonitorApplication.taskExecutor.execute(new ProcessingImageDataTask(image, dataComponent.getBorderX(), dataComponent.getBorderY(), false));
                            });
                        }
                    }
                    if(flag){
                        HardWareInfo.refreshAll = false;
                    }
                }
                Thread.sleep(1500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
